using System.Collections.Generic;
using UnityEngine;

namespace Yoozoo.Modules.InnerCity
{
    public class LodResPoint : LodBase
    {
        protected TileCollideManager m_tile_collide;

        public AnimationCurve scale_curve;

        public float m_startScale = 60f;    //开始缩放的高度
        public float max_scale = 30;
        
        public Transform ground;

        private GameObject m_mainCamera;
        
        private float m_ScaleFactor;

        public List<TileCollideItem> showList;

        public Transform hud;

        private bool groudActive = false;
        
        private void Awake()
        {
            this.m_tile_collide = base.GetComponent<TileCollideManager>();
            m_mainCamera = WorldCameraManager.mainCamera.gameObject;
            if (showList != null)
            {
                for (int i = 0; i < showList.Count; i++)
                {
                    m_tile_collide.Add(showList[i]);
                }
            }
            
            //SetGroundActive(true);
        }

        

        private void Update()
        {
            if (transform.position.y > 10000)    // 不可见的不处理
            {
                return;
            }
            
            float height = m_mainCamera.transform.position.y;
            
            if (height >= WorldEntry.DISTANCE_LOD2)
            {
                return;
            }
            
            if (height < WorldEntry.DISTANCE_LOD1)
            {
                SetGroundActive(true);
                UpdateLod();
            }
            else if (height >= WorldEntry.DISTANCE_LOD1)
            {
                SetGroundActive(false);
            }
        }

        public override void UpdateLod()
        {
//            // this.m_tile_collide.SetScale(Mathf.Min(this.scale_curve.Evaluate(GetLodDistance()/this.max_scale) * this.max_scale, this.max_scale), false);
//            // m_ScaleFactor = (float) (m_mainCamera.transform.localPosition.y - WorldEntry.DISTANCE_LOD0) / (WorldEntry.DISTANCE_LOD1 - WorldEntry.DISTANCE_LOD0);
//            // Debug.Log("m_ScaleFactor: " + m_ScaleFactor + "Evaluate" + scale_curve.Evaluate(m_ScaleFactor));
//            m_ScaleFactor = (float) (m_mainCamera.transform.localPosition.y - m_startScale) / (WorldEntry.DISTANCE_LOD0 - m_startScale);
//            this.m_tile_collide.SetScale(scale_curve.Evaluate(m_ScaleFactor), false);
//            SetGroundScale(scale_curve.Evaluate(m_ScaleFactor));
//            base.UpdateLod();
        }

        public int GetLodDistance()
        {
            return Mathf.FloorToInt(m_mainCamera.transform.localPosition.y / 10);
        }
        
        public static void SetMaxScaleS(LodResPoint self, float max_scale)
        {
            self.SetMaxScale(max_scale);
        }
        
        private void SetMaxScale(float max_scale)
        {
            this.max_scale *= max_scale;
        }

        public float GetCurrentCityScaleS(LodResPoint self)
        {
            return self.GetCurrentCityScale();
        }

        private float GetCurrentCityScale()
        {
            return this.scale_curve.Evaluate(GetLodDistance());
        }

        public void ForceUpdateScaleS(LodResPoint self)
        {
            //self.m_tile_collide.SetScale(Mathf.Min(self.scale_curve.Evaluate(GetLodDistance()), self.max_scale), true);
        }

        private void SetGroundActive(bool isActive)
        {
//            if (isActive!=groudActive)
//            {
//                ground.gameObject.SetActive(isActive);
//                groudActive = isActive;
//            }
        }

        private void SetGroundScale(float scale)
        {
            // 因为资源点现有资源的原因 屏蔽资源点缩放功能 hud调整缩放和lod1主城hud一样
            //var groundScale = 1f - scale;
            //ground.localScale = new Vector3(groundScale, groundScale, groundScale);
            //var hudScale = (Mathf.Abs(m_ScaleFactor)*2 + 1)*0.05f;

            hud.localScale = new Vector3(0.23307f, 0.23307f, 0.23307f);
            //hud.localScale = new Vector3(hudScale, hudScale, hudScale);
            //var posY = (1 - Mathf.Abs(m_ScaleFactor)) * 50f;
            // hud.localPosition = new Vector3(0,posY,0);
        }
    }
}